package com.hcms.admin.controller.cms;

import com.hcms.admin.service.ICmsPageService;
import com.hcms.common.api.CommonPage;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.CmsPage;
import com.hcms.mybatis.entity.SysAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 单页模块 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-03-31
 */
@Api("单页模块")
@RestController
@RequestMapping("/cmsPage")
public class CmsPageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ICmsPageService cmsPageService;

    @ApiOperation("获取单页记录")
    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CommonPage<CmsPage>>> index(
            @RequestParam(required = false) Long cateId,
            @RequestParam(required = false) Integer status,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        logger.info("Run index(), pageNum={}, pageSize={}", pageNum, pageSize);
        List<CmsPage> list = cmsPageService.getList(cateId, status, pageNum, pageSize);
        return CommonResponse.ok(CommonPage.restPage(list));
    }

    @ApiOperation("获取单页记录")
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<CmsPage>> view(@PathVariable Long id) {
        logger.info("Run view(), id={}", id);
        CmsPage page = cmsPageService.getView(id);
        if (page == null) {
            logger.error("The page does not exist");
            return CommonResponse.notFound("记录不存在");
        }
        return CommonResponse.ok(page);
    }

    @ApiOperation("添加记录")
    @PostMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CmsPage>> create(@RequestBody CmsPage entity) {
        logger.info("Run create()");
        int count = cmsPageService.createItem(entity);
        if (count > 0) {
            logger.info("Adding succeeded, id={}", entity.getId());
            return CommonResponse.created(entity);
        } else {
            logger.error("Adding failed");
            return CommonResponse.badRequest("添加失败");
        }
    }

    @ApiOperation("更新记录")
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysAdmin>> update(@PathVariable Long id, @RequestBody CmsPage entity) {
        logger.info("Run update(), id={}", id);
        int count = cmsPageService.updateItem(id, entity);
        if (count > 0) {
            logger.info("Update succeeded");
            return CommonResponse.accepted("更新成功");
        } else {
            logger.error("Update failed");
            return CommonResponse.badRequest("更新失败");
        }
    }

    @ApiOperation("删除记录")
    @DeleteMapping("/{ids}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@PathVariable String ids) {
        logger.info("Run delete(), ids={}", ids);
        int count = cmsPageService.deleteItem(ids);
        if (count > 0) {
            logger.info("Delete succeeded");
            return CommonResponse.noContent("删除成功");
        } else {
            logger.info("Delete failed");
            return CommonResponse.badRequest("删除失败");
        }
    }

}
